using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using JGSY.CMS.LowCode.Platform.Domain.Interfaces;
using JGSY.CMS.LowCode.Platform.Infrastructure.Repositories;
using JGSY.CMS.LowCode.Platform.Infrastructure.UnitOfWork;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Extensions
{
    /// <summary>
    /// 基础设施仓储层服务注册扩展
    /// </summary>
    public static class RepositoryServiceExtensions
    {
        /// <summary>
        /// 注册所有仓储服务
        /// </summary>
        /// <param name="services">服务容器</param>
        /// <returns>服务容器</returns>
        public static IServiceCollection AddRepositories(this IServiceCollection services)
        {
            // 工作单元
            services.AddScoped<IUnitOfWork, UnitOfWork.UnitOfWork>();

            // 核心业务仓储
            services.AddScoped<IArticleRepository, ArticleRepository>();
            services.AddScoped<ICategoryRepository, CategoryRepository>();
            services.AddScoped<IUserRepository, UserRepository>();
            services.AddScoped<IRoleRepository, RoleRepository>();
            services.AddScoped<IDepartmentRepository, DepartmentRepository>();
            services.AddScoped<ITeamRepository, TeamRepository>();
            services.AddScoped<ITeamMemberRepository, TeamMemberRepository>();
            services.AddScoped<ITemplateRepository, TemplateRepository>();
            services.AddScoped<IProductRepository, ProductRepository>();
            services.AddScoped<IContentRepository, ContentRepository>();

            // 表单相关仓储
            services.AddScoped<IFormRepository, FormRepository>();
            services.AddScoped<IFieldRepository, FieldRepository>();
            services.AddScoped<IFormSubmissionRepository, FormSubmissionRepository>();

            // 页面和媒体仓储
            services.AddScoped<IPageRepository, PageRepository>();
            services.AddScoped<IMediaFileRepository, MediaFileRepository>();

            // 系统管理仓储
            services.AddScoped<ISystemSettingRepository, SystemSettingRepository>();
            services.AddScoped<IAuditLogRepository, AuditLogRepository>();
            services.AddScoped<ISecurityEventRepository, SecurityEventRepository>();
            services.AddScoped<ILoginHistoryRepository, LoginHistoryRepository>();
            services.AddScoped<IUserDeviceRepository, UserDeviceRepository>();
            services.AddScoped<ITwoFactorRepository, TwoFactorRepository>();
            services.AddScoped<IRiskAssessmentRepository, RiskAssessmentRepository>();
            services.AddScoped<IPublishRepository, PublishRepository>();

            // 工作流仓储
            services.AddScoped<IWorkflowRepository, WorkflowRepository>();
            services.AddScoped<IWorkflowInstanceRepository, WorkflowInstanceRepository>();
            services.AddScoped<IWorkflowTaskRepository, WorkflowTaskRepository>();

            // 权限管理仓储
            services.AddScoped<IPermissionRepository, PermissionRepository>();
            services.AddScoped<IUserRoleRepository, UserRoleRepository>();

            return services;
        }

        /// <summary>
        /// 注册仓储相关的日志服务
        /// </summary>
        /// <param name="services">服务容器</param>
        /// <returns>服务容器</returns>
        public static IServiceCollection AddRepositoryLogging(this IServiceCollection services)
        {
            services.AddLogging(builder =>
            {
                // 从已注册的配置中添加日志配置
                var serviceProvider = services.BuildServiceProvider();
                var configuration = serviceProvider.GetRequiredService<Microsoft.Extensions.Configuration.IConfiguration>();
                builder.AddConfiguration(configuration.GetSection("Logging"));
                builder.AddConsole();
                builder.AddDebug();
            });

            return services;
        }

        /// <summary>
        /// 注册完整的基础设施服务
        /// </summary>
        /// <param name="services">服务容器</param>
        /// <returns>服务容器</returns>
        public static IServiceCollection AddInfrastructure(this IServiceCollection services)
        {
            return services
                .AddRepositoryLogging()
                .AddRepositories();
        }
    }
}
